package com.hs.myshell.demo.swt;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

import javax.swing.Timer;
import javax.xml.stream.events.Characters;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.google.common.base.Ascii;
 
 
public class CanvasSample {
	private Image image = null;
	private Shell shell = null;
	private Canvas canvas = null;
	
	Timer myRepaintTimer = null;
	protected int i;
	
	public CanvasSample(){
		createContent();
	}
	
	public void createContent(){
		shell = new Shell();
		shell.setLayout(new FillLayout());
//		image = new Image(shell.getDisplay(), getClass().getResourceAsStream("1.gif"));
		canvas = new Canvas(shell, SWT.NONE);
		Display currentDisplay = Display.getCurrent();
		canvas.setBackground(currentDisplay.getSystemColor(SWT.COLOR_WHITE));
		//光标
		Point myCursorCoordinates = new Point(0, 0);
		canvas.addPaintListener(new PaintListener(){
			public void paintControl(PaintEvent e){
				//绘制图像
//				e.gc.drawImage(image, 10, 10);
				//绘制椭圆
//				e.gc.drawOval(20, 20, 200, 100);
				//绘制直线
//				e.gc.drawLine(20,10,100,20);
				
//				Font font = new Font(e.display, "Arial", 14, SWT.BOLD | SWT.ITALIC);
				Font font = new Font(e.display, "Consolas", 10, SWT.NONE);
				e.gc.setFont(font);
				//绘制矩形
				Point fontSize = e.gc.textExtent("W");
				int row = 0;
				int col = 0+fontSize.x;
				final MyCursor myCursor = new MyCursor();
				myCursor.setCol(col);
				myCursor.setRow(row);
				//绘制字符串
//				e.gc.drawString("h", col, 0);
				//这个单位是像素,如果要在Y坐标下移一个的话，要乘以当前字体的高度
				
				String ss= "[root@iZw2e6dprbZ ~]#";
//				String ss= "r";
				char[] charArray = ss.toCharArray();
				for (char c : charArray) {
					if(Ascii.CR == c) {
						System.out.println("CR,回车,把光标移到0坐标");
						col = 0;
						myCursor.setCol(col);
						continue;
					}
					if(Ascii.LF == c) {
						row=(row+1)*fontSize.y;
						myCursor.setRow(row);
						System.out.println("LF,换行,把光标移到Y坐标+1");
						continue;
					}
					e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_WHITE));
					e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_BLACK));
//					e.gc.fillRectangle(col, 0, fontSize.x-5, fontSize.y-5);
					e.gc.drawString(String.valueOf(c), col, 0);
					col+=fontSize.x;
					myCursor.setCol(col);
				}
				
				//绘制光标
				if (i%2==1) {
					e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLACK));
				}else {
					e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_WHITE));
				}
				i++;
				e.gc.fillRectangle(myCursor.getCol(), myCursor.getRow(), fontSize.x, fontSize.y);
				Toolkit.getDefaultToolkit().beep();
				
				
				
//				//设置display前景色
//				e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_GREEN));
//				//设置display字体
//				e.gc.setFont(font);
//				//设置display 背景色
//				e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_GRAY));
//				//再绘制字符串
//				e.gc.drawString("T", 30, 50);
				
				
//				font.dispose();
			}
		});
		
		if (myRepaintTimer != null) {
			myRepaintTimer.stop();
		}
		myRepaintTimer = new Timer(1000,new RePaintTimer(this));
		myRepaintTimer.start();
		
		shell.setSize(700, 550);
	}
	
	
	
	public Canvas getCanvas(){
		return canvas;
	}
	public void setCanvas(Canvas canvas){
		this.canvas = canvas;
	}
	public Image getImage(){
		return image;
	}
	public void setImage(Image image){
		this.image = image;
	}
	public Shell getShell(){
		return shell;
	}
	public void setShell(Shell shell){
		this.shell = shell;
	}
	public static void main(String[] args){
		test();
		Display display = Display.getDefault();
		CanvasSample cSample = new CanvasSample();
//		//定时更新光标位置,这里通过 定时调度器来刷新不好，应该是 根据底层的数据更新光标的x.y坐标，然后按需更新光标的位置
//		display.timerExec(1000,new Runnable() {
//			public void run() {
//				cSample.updateCursor();
//			}
//		});
		
		cSample.getShell().open();
		while(!cSample.getShell().isDisposed()){
			if(!display.readAndDispatch())
				display.sleep();
		}
//		cSample.getImage().dispose();
		display.dispose();
	}
	
	class RePaintTimer implements ActionListener{
		CanvasSample canvasSample;

		public RePaintTimer(CanvasSample canvasSample) {
			super();
			this.canvasSample = canvasSample;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println("redraw... shell");
			Display current = Display.getCurrent();
			if (current != null) {
				current.asyncExec(new Runnable() {
					
					@Override
					public void run() {
						if (shell != null && !shell.isDisposed()) {
							shell.redraw();
						}
					}
				});
			}
		}
		
		
	}
	
	/**
	 * 
	 */
	protected void updateCursor() {
		System.out.println("更新光标");
	}

	/**
	 * 分析Java中的全宽或半宽字符(Analyzing full width or half width character in Java)
	 * 我想分析char数组中的全宽或半宽字符。
例如：
char [] password = {'t'，'e'，'s'，'t'，'思'，'题'};
已满此char数组中的宽度和半宽字符。
half width = t，e，s，t
full width =思，题
那么，如何分析java中char数组的全宽或半宽？
非常感谢！
解决方案
JDK包含一个提及完整/半宽的类： InputSubset
http://docs.oracle.com/javase/7/docs/api/java/awt/im/InputSubset.html
不幸的是，没有办法检查哪个字符落在哪个子集中。
尽管如此，显然全/半宽是一个定义明确的unicodes概念。互联网上可能存在准确的规范。
http ：//en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms
http://en.wikipedia.org/wiki/DBCS

我想这对你的用例来说已经足够了要说的是，0x00-0xFF字符是半宽的;其他字符是全宽的，除了unicode块中的半宽字符"Halfwidth and Fullwidth Forms"
  boolean isHalfWidth（ char c）
 {
返回'\ u0000'< = c&& c< ='\ u00FF'
 || '\ uFF61'< = c&& c< ='\ uFFDC'
 || '\ uFFE8'< = c&& c< ='\ uFFEE'; 
} 
  

原文
I would like to analyze full width or half width character in char array.

for example:

char [] password = {'t','e','s','t','思','題'};

There are full width and half width characters in this char array.

half width = t,e,s,t

full width = 思,題

So, how can I analyze full width or half width for char array in java?

Thanks a lot!

解决方案
JDK contains one class that mentions full/half width: InputSubset

http://docs.oracle.com/javase/7/docs/api/java/awt/im/InputSubset.html

Unfortunately there's no method to check which char falls in which subset.

Nonetheless, apparently full/half width is a well defined concept for unicodes. There maybe an accurate spec somewhere on internet.

http://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms

http://en.wikipedia.org/wiki/DBCS

I guess it'll be good enough for your use case to say that, 0x00-0xFF chars are half-width; other chars are full-width, except the half-width chars in the unicode block "Halfwidth and Fullwidth Forms"

boolean isHalfWidth(char c)
{
    return '\u0000' <= c && c <= '\u00FF'
        || '\uFF61' <= c && c <= '\uFFDC'
        || '\uFFE8' <= c && c <= '\uFFEE' ;
}
	 */
	public static void test() {
		char [] password = {'t','e','s','t','思','題'};
//		char [] password = {'1','12'};
		for (char c : password) {
			boolean halfWidth = isHalfWidth(c);
			System.out.println(c +" isHalfWidth: " + halfWidth);
		}
//		There are full width and half width characters in this char array.

//		half width = t,e,s,t

//		full width = 思,題
	}
	static boolean isHalfWidth(char c)
	{
	    return '\u0000' <= c && c <= '\u00FF'
	        || '\uFF61' <= c && c <= '\uFFDC'
	        || '\uFFE8' <= c && c <= '\uFFEE' ;
	}
}